import { observable, action } from 'mobx';
import Api from '../../apis/monitor';
import ListBaseStore from './../ListBaseStore';
import { WeaLogView } from 'comsMobx';

const { LogStore } = WeaLogView;
/**
 * 邮件 - 邮件监控
 */
export default class EmailMonitorStore extends ListBaseStore {
  @observable loading = false;
  @observable isDeleteing = false;
  @observable logStore = new LogStore();
  @action setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  };
  @observable logstatus = {
    targetId: '',
    logType: '', // 日志相关参数
    logSmallType: '',
  };
  // 加载列表数据
  @action doTableLoading = (params) => {
    this.showLoading(true);
    let resultParams = this.getFormParams();
    let commonParams = {};
    resultParams = { ...resultParams, ...commonParams, ...params };

    Api.monitorList(resultParams).then((result) => {
      this.viewRight = result.viewRight == 1;
      if (this.viewRight) {
        this.logstatus = {
          ...this.logstatus,
          logType: result.logType,
          logSmallType: result.logSmallType,
        };
        const sessionkey = result.sessionkey;
        this.sessionkey = sessionkey;
        this.comsWeaTableStore.getDatas(sessionkey, 1);
      }

      this.setViewRightInitCompleted(); // 要放在后面，防止有权限时，初始化慢导致无权限提示闪一下

      this.showLoading(false);
    });
  };

  // 初始化邮件监控页面查询条件
  @action initConditionDatas = (params, callback) => {
    Api.monitorCondition(params).then((result) => {
      this.conditioninfo = result.conditioninfo;
      this.initFormFields(this.conditioninfo); // 一定要先初始化initFormFields表单对象
      if (callback && typeof callback === 'function') callback();
    });
  };

  // 批量删除
  @action deleteMailFromMonitor = (params, callback) => {
    this.isDeleteing = true;
    Api.deleteMailFromMonitor(params).then(() => {
      this.isDeleteing = false;
      if (callback && typeof callback === 'function') {
        callback();
      }
    });
  };
}
